package com.jason.lucene.example;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Objects;

/**
 * 遍历文件系统和索引文本文件
 * @author he peng
 * @create 2017/11/29 11:40
 * @see
 */
public class Indexer {

    public static void main(String[] args) throws IOException {

        // 遍历一个目录 ， 索引其中所有的 .txt 文件

        long startTime = System.currentTimeMillis();

        Path indexDirPath = Paths.get("C://lucene//index");
        Path dataDirPath = Paths.get("D://");

        index(indexDirPath , dataDirPath);

        long endTime = System.currentTimeMillis();

        System.err.println("cost time = " + (endTime - startTime) + " ms");
    }

    public static void index(Path indexDirPath , Path dataDirPath) throws IOException {
        File dataDirFile = dataDirPath.toFile();
        boolean isException = ! dataDirFile.exists() || ! dataDirFile.isDirectory();
        if (isException) {
            throw new IllegalArgumentException(dataDirPath + " does not exist or is not directory.");
        }

        Directory directory = FSDirectory.open(indexDirPath);
        IndexWriterConfig config = new IndexWriterConfig(new StandardAnalyzer());
        config.setInfoStream(System.out);
        IndexWriter indexWriter = new IndexWriter(directory , config);
        indexDirectory(indexWriter , dataDirPath.toFile());
        indexWriter.close();
    }

    public static void indexDirectory(IndexWriter indexWriter , File dataDirFile) throws IOException {
        File[] files = dataDirFile.listFiles();
        if (Objects.isNull(files)) {
            return;
        }
        for (int i = 0 ; i < files.length; i++ ) {
            File file = files[i];
            if (Objects.isNull(file)) {
                continue;
            }
            if (file.isDirectory()) {
                indexDirectory(indexWriter , file);
            } else if (file.getName().endsWith(".txt")) {
                indexFile(indexWriter , file);
            }
        }
    }

    public static void indexFile(IndexWriter indexWriter , File file) throws IOException {
        if (! file.exists() || ! file.canRead() || file.isHidden()) {
            System.out.println("file " + file.getCanonicalPath()
                        + " not exist or can not read or is hidden");
            return;
        }

        System.out.println("indexing " + file.getCanonicalPath());
        Document doc = new Document();
        doc.add(new Field("contents", new FileReader(file) , TextField.TYPE_NOT_STORED));
        doc.add(new Field("filename", file.getCanonicalPath() , TextField.TYPE_STORED));
        indexWriter.addDocument(doc);
    }

}
